# Author Damian 'Rush' Kaczmarek
# License MIT

# TODO:
# Parsing XML output instead of text output and provide changed file statistics.

package svnNotify;
use base Module;
use strict;
use utf8;

my $repository = Config::get('repository');
my $svntimeout = Config::get('svntimeout', '10');
my $refresh    = Config::get('refreshtime', '20');

my $lastrevision = 0;

my $firstloop = 1;

sub check_svn
{
	(my $retcode, my @output) =
	  Util::run_command("svn log --revision " . ($lastrevision + 1) . ":HEAD " . $repository . "2>/dev/null",
		$svntimeout);
	my @svnlog;
	foreach (@output) {
		next if (m/^(\-)+$/ or m/^$/);    # Skip empty and separating lines
		if (m/^r(\d+) \| (\S+)/) {        # Revision number and author line
			$lastrevision = $1;
			next if ($firstloop);
			Core::send_msg("[svn] " . Util::color_nick($2) . " * r" . $1);
		}
		else {
			next if ($firstloop);
			Core::send_msg("[svn] " . $_);
		}
	}
	close(SVNLOG);
	if ($lastrevision > 1) {                  # Allow displaying only if we actually got any results before
		$firstloop = 0;
	}
}

Timer::set(\&check_svn, int($refresh), 1);
